﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class PerchaseAdd : System.Web.UI.Page
    {
        protected EmpBasicInfo ebi;
        protected void Page_Load(object sender, EventArgs e)
        {
            ebi = Session["ebi"] as EmpBasicInfo;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqPerchaseMain reqPerchaseMain = new ReqPerchaseMain();
            string maxFormNo = ctx.ReqPerchaseMains.Max(r => r.FormNo);
            string formNo = "Form_Perc_000001";
            if (!string.IsNullOrEmpty(maxFormNo))
            {
                formNo = "Form_Perc_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
            }
            reqPerchaseMain.FormNo = formNo;
            reqPerchaseMain.ReqDate = DateTime.Now;
            reqPerchaseMain.EmpId = ebi.Id;
            reqPerchaseMain.Cost = int.Parse(Cost.Value);
            reqPerchaseMain.Cause = Cause.Value;
            ctx.ReqPerchaseMains.InsertOnSubmit(reqPerchaseMain);
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + reqPerchaseMain.FormNo + "';window.close();</script>");
        }
    }
}